﻿using BaseUI;
using BLL;
using Model;
using System;
using System.Data;
using System.Web.UI.WebControls;

namespace Thksoft.BusinessExpansion.Templete
{
    public partial class Upd_Module : BasePage
    {
        private Sys_ModuleBLL smb = new Sys_ModuleBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindModule();

                if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
                {
                    //修改
                    Bind();

                    ListModuleQueue.Visible = true;
                    btnUp.Visible = true;
                    btnDown.Visible = true;
                }
            }

            if (Stutar.Value.ToString().Trim() == "Add_Module")
            {
                Add();
                Stutar.Value = "";
            }
        }

        private void BindModule()
        {
            DataTable dataModul = smb.GetAllInfo("");
            PublicMethod.BindDropDownList(ddlModule, dataModul, "MODULE_ID", "MODULE_NAME", "MODULE_PARENTID", "-1", true);
        }

        /// <summary>
        /// 绑定数据
        /// </summary>
        private void Bind()
        {
            Sys_Module model = smb.GetInfoById(Request.QueryString["ID"].ToString());
            ddlModule.SelectedValue = model.MODULE_PARENTID.ToString();
            txtName.Value = model.MODULE_NAME;
            txtPath.Value = model.MODULE_PATH;
            ddlState.SelectedValue = model.MODULE_STATE;
            ListModuleQueue.Items.Clear();

            DataTable dt = smb.GetFatherModules(model.MODULE_PARENTID.ToString());
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                var item = new ListItem(dt.Rows[i]["MODULE_NAME"].ToString(), dt.Rows[i]["MODULE_ID"].ToString());
                ListModuleQueue.Items.Add(item);
            }
        }

        /// <summary>
        /// 添加
        /// </summary>
        private void Add()
        {
            Sys_Module model = new Sys_Module();
            if (string.IsNullOrEmpty(txtName.Value.ToString().Trim()))
            {
                MessageBox.Show(Page, "请输入模块名称");
                return;
            }
            if (string.IsNullOrEmpty(txtPath.Value.ToString().Trim()))
            {
                MessageBox.Show(Page, "请输入模块路径");
                return;
            }

            string orderByStr = smb.OrderByNum(ddlModule.SelectedValue.ToString());

            model.MODULE_NAME = txtName.Value.ToString().Trim();

            model.MODULE_STATE = ddlState.SelectedValue;
            model.MODULE_PARENTID = decimal.Parse(ddlModule.SelectedValue.ToString());

            model.MODULE_PATH = txtPath.Value.Trim(); ;

            if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
            {
                model.MODULE_ID = decimal.Parse(Request.QueryString["ID"]);
                //修改
                int count = smb.UPDATE(model, lognUser.USER_NAME);

                if (count > 0)
                {
                    for (int i = 0; i < ListModuleQueue.Items.Count; i++)
                    {
                        var lastitem = ListModuleQueue.Items[i];

                        if (model.MODULE_PARENTID == -1)
                        {
                            smb.UpdateByOrd(lastitem.Value, i + 1, lognUser.USER_NAME);
                        }
                        else
                        {
                            smb.UpdateByOrd2(lastitem.Value, i + 1, lognUser.USER_NAME);
                        }
                        //lastitem.Value;
                    }

                    MessageBox.Show(Page, "保存成功");
                }
                else
                {
                    MessageBox.Show(Page, "保存失败");
                }
            }
            else
            {
                //新增
                if (model.MODULE_PARENTID == -1)
                {
                    model.MODULE_ORDERNUM1 = decimal.Parse((Convert.ToInt32(orderByStr.Split('-')[0].ToString()) + 1).ToString());

                    model.MODULE_ORDERNUM2 = 0;
                }
                else
                {
                    if (orderByStr.ToString() == "-")
                    {
                        model.MODULE_ORDERNUM1 = smb.GetInfoById(ddlModule.SelectedValue.ToString()).MODULE_ORDERNUM1;
                        model.MODULE_ORDERNUM2 = 1;
                    }
                    else
                    {
                        model.MODULE_ORDERNUM1 = Convert.ToInt32(orderByStr.Split('-')[0].ToString());
                        model.MODULE_ORDERNUM2 = decimal.Parse((Convert.ToInt32(orderByStr.Split('-')[1].ToString()) + 1).ToString());
                    }
                }

                int count = smb.INSERT(model, lognUser.USER_NAME);

                if (count > 0)
                {
                    MessageBox.Show(Page, "新增成功");
                }
                else
                {
                    MessageBox.Show(Page, "新增失败");
                }
            }

            ModuleParId.Value = "";
        }

        protected void btnUp_Click(object sender, EventArgs e)
        {
            int selectedIndex = ListModuleQueue.SelectedIndex;
            if (selectedIndex > 0)
            {
                //获取上一个
                var lastitem = ListModuleQueue.Items[selectedIndex];
                ListModuleQueue.Items.RemoveAt(selectedIndex);//移除
                ListModuleQueue.Items.Insert(selectedIndex - 1, lastitem);//添加
            }
            else
            {
                MessageBox.Show(Page, "已经是最上面的位置了，不能再向上移了。");
                return;
            }
        }

        protected void btnDown_Click(object sender, EventArgs e)
        {
            int maxNum = ListModuleQueue.Items.Count;
            int selectedIndex = ListModuleQueue.SelectedIndex;
            if (ListModuleQueue.SelectedValue == null || ListModuleQueue.SelectedValue == "")
            {
                MessageBox.Show(Page, "请先选择需要移动的内容！");
                return;
            }
            if (selectedIndex < maxNum - 1)
            {
                //获取下一个
                var lastitem = ListModuleQueue.Items[selectedIndex];

                ListModuleQueue.Items.RemoveAt(selectedIndex);//移除
                ListModuleQueue.Items.Insert(selectedIndex + 1, lastitem);//添加
            }
            else
            {
                MessageBox.Show(Page, "已经是最下面的位置了，不能再向下移了。");
                return;
            }
        }
    }
}